import { Space, message, Divider, Modal } from 'antd'
import { ExclamationCircleOutlined } from '@ant-design/icons'
import { AccountSchema } from '@src/components/schema/account'
import { DepartmentSchema } from '@src/components/schema/departmentSchema'
import { ColumnType } from 'antd/lib/table'
import { getPersonDetailRequest, deleteAccountRequest } from './ajax'
import { awaitWrap } from '@src/assets/js/tool'

const { confirm } = Modal

// 删除二次确认
function confrimDelete (id: string, refreshData: () => void): void {
  confirm({
    icon: <ExclamationCircleOutlined />,
    content: '您确认要删除吗？',
    onOk: () => {
      delHandle(id, refreshData).catch(e => console.error(e))
    }
  })
}

async function delHandle (id: string, refreshData: () => void): Promise<void> {
  const hide = message.loading('删除中', 120)
  const [e] = await awaitWrap(deleteAccountRequest(id))
  hide()
  if (e === null) {
    refreshData()
  }
}

// 获取详情
async function getPersonDetail (id: string, setModifyData: (v: AccountSchema) => void, setVisible: (v: boolean) => void): Promise<void> {
  const h = message.loading('加载中', 120)
  const [e, d] = await awaitWrap(getPersonDetailRequest(id))
  h()
  if (e === null && d !== null) {
    setModifyData(d)
    setVisible(true)
  }
}

export function getColumn (departmentMap: { [k: string]: DepartmentSchema }, setModifyData: (v: AccountSchema) => void, setVisible: (v: boolean) => void, setRoleVisible: (v: boolean) => void, setDefaultVisible: (v: boolean) => void, refreshData: () => void): Array<ColumnType<any>> {
  const columns = [
    {
      title: '姓名',
      dataIndex: 'realname',
      key: 'realname'
    },
    {
      title: '部门',
      dataIndex: 'structureid',
      key: 'structureid',
      render: (v: string) => {
        const o = departmentMap[v]
        if (o !== undefined) {
          return o.name
        }
      }
    },
    {
      title: '职务',
      dataIndex: 'post',
      key: 'post'
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      render: (v: number) => {
        if (v === 0) {
          return '离职'
        } else if (v === 1) {
          return '在职'
        }
      }
    },
    {
      title: '账号',
      dataIndex: 'username',
      key: 'username'
    },
    {
      title: '操作',
      dataIndex: 'operate',
      key: 'operate',
      render: (v: undefined, l: AccountSchema) => {
        return (
          <Space size={0} split={<Divider type="vertical" />}>
            <a onClick={() => { getPersonDetail(l.id as string, setModifyData, setVisible).catch(e => console.error(e)) }}>编辑</a>
            <a onClick={() => { getPersonDetail(l.id as string, setModifyData, setRoleVisible).catch(e => console.error(e)) }}>绑定角色</a>
            <a onClick={() => { getPersonDetail(l.id as string, setModifyData, setDefaultVisible).catch(e => console.error(e)) }}>设置默认桌面</a>
            <a onClick={() => { confrimDelete(l.id as string, refreshData) }}>删除</a>
          </Space>
        )
      }
    }
  ]
  return columns
}
